var blackMarble;

function InitRollerShutter() {
    blackMarble = 1;
    refreshRollerShutterLayer();
    $("#RollerShutterRefresh").click(function() {
        refreshRollerShutterLayer();
    });
    $("#RollerShutterClear").click(function() {
        clearRollerShutter();
    });
    $("#RollerShutterHandle").click(function() {
        InitRollerShutterHandle();
    });
}

function InitRollerShutterHandle() {
    for (var attr in cesium.layer3DList) {
        var layerInfo = cesium.layer3DList[attr];
        if (layerInfo && (layerInfo["SERVICETYPE"] == sw_servicetype.sw_tile || layerInfo["SERVICETYPE"] == sw_servicetype.sw_vector)) {
            layerInfo.splitDirection = 0;
        }
    }
    //for (var i = 0; i < m_MapConfigLayers["maplayers"].length; i++) {
    //    var layerInfo = m_MapConfigLayers["maplayers"][i];
    //    if ((layerInfo.type == "4" || layerInfo.type == "8") && cesium.layer3DList[layerInfo.id]) {
    //        cesium.layer3DList[layerInfo.id].splitDirection = 0;
    //    }
    //}
    var layerId = $('#RollerShutterLayer').val();
    if (!layerId) {
        showMsgLayer("图层为空");
        return;
    }
    var type = parseInt($('#RollerShutterType').val());
    //var layers = viewer.imageryLayers;
    //layers._layers[2].splitDirection = Cesium.ImagerySplitDirection.LEFT;
    cesium.layer3DList[layerId].splitDirection = type;
    var slider = document.getElementById("SplitSlider");
    $("#SplitSlider").show();
    var splittips = document.getElementById("splitTips")
    splittips.style.display = "block";
    viewer.scene.imagerySplitPosition =
        slider.offsetLeft / slider.parentElement.offsetWidth;

    var handler = new Cesium.ScreenSpaceEventHandler(slider);

    var moveActive = false;

    function move(movement) {
        if (!moveActive) {
            return;
        }

        var relativeOffset = movement.endPosition.x;
        var splitPosition =
            (slider.offsetLeft + relativeOffset) /
            slider.parentElement.offsetWidth;
        slider.style.left = 100.0 * splitPosition + "%";
        viewer.scene.imagerySplitPosition = splitPosition;
    }

    handler.setInputAction(function() {
        moveActive = true;
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
    handler.setInputAction(function() {
        moveActive = true;
    }, Cesium.ScreenSpaceEventType.PINCH_START);

    handler.setInputAction(move, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    handler.setInputAction(move, Cesium.ScreenSpaceEventType.PINCH_MOVE);

    handler.setInputAction(function() {
        moveActive = false;
    }, Cesium.ScreenSpaceEventType.LEFT_UP);
    handler.setInputAction(function() {
        moveActive = false;
    }, Cesium.ScreenSpaceEventType.PINCH_END);
    switch (type) {
        case -1: //左侧
            $("#splitTips").css("left", "-150px");
            break;
        case 1: //右侧
            $("#splitTips").css("left", 0);
            break;
    }
    $("#splitTips").html($('#RollerShutterLayer option:selected').text());
}

function refreshRollerShutterLayer() {
    var html = '';
    //for (var i = 0; i < m_MapConfigLayers["maplayers"].length; i++) {
    //    var layerInfo = m_MapConfigLayers["maplayers"][i];
    //    if ((layerInfo.type == "4" || layerInfo.type == "8") && cesium.layer3DList[layerInfo.id]) {
    //        html += "<option value='" + layerInfo.id + "'>" + layerInfo.name + "</option>"; //拼接option中的内容
    //    }
    //}
    for (var attr in cesium.layer3DList) {
        var layerInfo = cesium.layer3DList[attr];
        if (layerInfo["SERVICETYPE"] == sw_servicetype.sw_tile || layerInfo["SERVICETYPE"] == sw_servicetype.sw_vector) {
            html += "<option value='" + attr + "'>" + layerInfo.NAME + "</option>"; //拼接option中的内容
        }
    }
    $("#RollerShutterLayer").html(html); //将option的拼接内容加入select中，注意选择器是select的ID
    form.render('select'); //重点：重新渲染select
}

function clearRollerShutter() {
    for (var attr in cesium.layer3DList) {
        var layerInfo = cesium.layer3DList[attr];
        if (layerInfo && (layerInfo["SERVICETYPE"] == sw_servicetype.sw_tile || layerInfo["SERVICETYPE"] == sw_servicetype.sw_vector)) {
            layerInfo.splitDirection = 0;
        }
    }
    $("#SplitSlider").hide();
    viewer.scene.imagerySplitPosition = 0;
}